<template>
	<view>
		<uni-popup ref="popup" type="bottom">
			<view class="cart-content" v-if="goodsCart.length > 0">
				<view class="cart-banner">
					<view style="display: flex; align-items: center;">
						<span class="vertical-bar"></span>
						<view class="legend">已选商品</view>
					</view>
					<view style="display: flex; align-items: center;" @click="cleargoodsCart()">
						<view class="claer-icon">
							<image style="width: 100%; height: 100%;" :src="icons.clear"></image>
						</view>
						<view class="legend">清空购物车</view>
					</view>
				</view>
				<view class="cart-show">
					<template v-for="(items, index) in goodsCart">
						<view class="cart-list" v-if="items.number > 0" :key="index">
							<view class="cart-mess">
								<view class="cart-name">{{ items.goods_name }}</view>
								<view>￥ {{ items.price }}</view>
							</view>
							<view class="cart-count">
								<view class="cart-reduce">
									<image style="width: 100%; height: 100%;" mode="aspectFit" :src="icons.cartReduce" @click="cartCount('reduce', items, index)"></image>
								</view>
								<input v-model="items.number" class="number-input" type="number" />
								<view class="cart-add">
									<image style="width: 100%; height: 100%;" mode="aspectFit" :src="icons.cartAdd" @click="cartCount('add', items, index)"></image>
								</view>
							</view>
						</view>
					</template>
				</view>
			</view>
		</uni-popup>
		<!-- 购物车 -->
		<view class="shop-flex">
			<view class="shop-cart">
				<div class="statistics">
					<view class="cart-botton" @click="open()"><span class="cart-number">{{ allNumber }}</span></view>
					<view class="whole">合计：{{ allMoney }}</view>
				</div>
				<div class="accounts" @click="settlement()">结算</div>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		mapGetters
	} from 'vuex';
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	export default {
		components: {
			uniPopup
		},
		props: ['toPage'],
		data() {
			return {
				show: false,
				icons: {
					clear: '/static/images/icons/icon_qk@3x.png',
					cartReduce: '/static/images/icons/icon_js@3x.png',
					cartAdd: '/static/images/icons/icon_tj_2@3x.png'
				}
			};
		},
		computed: {
			...mapGetters([
				'goodsCart'
			]),
			allNumber: function() {
				const data = this.goodsCart;
				let number = 0;
				data.forEach(element => {
					number += parseInt(element.number);
				})
				return number
			},
			allMoney: function() {
				const data = this.goodsCart;
				let money = 0;
				data.forEach(element => {
					money += element.number * element.price;
				})
				return money.toFixed(2);
			}
		},
		methods: {
			open() {
				if (this.goodsCart.length > 0) {
					if (!this.show) {
						this.$refs.popup.open();
						this.show = true;
					} else {
						this.$refs.popup.close();
						this.show = false;
					}
				}
			},
			settlement() {
				const _this = this;
				if (this.goodsCart.length > 0) {
					uni.navigateTo({
						url: '/pages/order/verify' + '?' + _this.version
					});
				} else {
					uni.showToast({
						icon: "none",
						title: '没有选购餐品',
						duration: 2000
					});
				}
			},
			cartCount(name, items, index) {
				if (name === 'reduce') {

					const number = this.goodsCart[index].number;
					// this.goodsCart.number--;

					let shopData = {
						index: index,
						type: 4 // 判断是哪种类型的操作 1.新增商品，2.新增数量，3.减少数量，4.减少商品
					}

					if (items.number > 1) {
						shopData.type = 3
					}
					// 在vuex里面做赋值
					this.$store.commit('addToCart', shopData);
					if (this.goodsCart.length === 0) {
						this.$refs.popup.close();
					}
				} else {
					if (items.number < items.stock) {
						const shopData = {
							index: index,
							type: 2 // 判断是哪种类型的操作 1.新增商品，2.新增数量，3.减少数量
						}
						this.$store.commit('addToCart', shopData);
					} else {
						uni.showToast({
							icon: "none",
							title: '库存不足',
							duration: 2000
						});
					}
				}
			},
			cleargoodsCart() {
				const shopData = {
					type: 5 // 判断是哪种类型的操作 1.新增商品，2.新增数量，3.减少数量，4.减少商品，5.清空购物车
				}
				this.$store.commit('addToCart', shopData);
				this.$refs.popup.close();
				this.show = false;
			}
		}
	};
</script>
<style>
	.shop-flex {
		position: fixed;
		bottom: 0;
		width: 100%;
		z-index: 99;
		background-color: #ffffff;
		padding-bottom: 60rpx;
	}

	.shop-cart {
		display: flex;
		align-items: center;
		margin: 0 30rpx;
	}

	.statistics {
		flex: 1;
		display: flex;
		align-items: center;
		height: 98rpx;
		background: rgba(51, 51, 51, 1);
		box-shadow: 0rpx 5rpx 10rpx 0rpx rgba(0, 0, 0, 0.2);
		border-radius: 49rpx 0rpx 0rpx 49rpx;
	}

	.cart-botton {
		width: 80rpx;
		height: 80rpx;
		background: url('~@/static/images/icons/cart@3x.png') no-repeat;
		background-size: 100% 100%;
		margin: 0 19rpx 0 9rpx;
	}

	.whole {
		display: flex;
		align-items: center;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #ffffff;
	}

	.accounts {
		width: 160rpx;
		height: 98rpx;
		border-top-right-radius: 49rpx;
		border-bottom-right-radius: 49rpx;
		background-color: #ffc324;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		background: linear-gradient(0deg, rgba(255, 195, 36, 1), rgba(255, 211, 94, 1));
		box-shadow: 0rpx 5rpx 10rpx 0rpx rgba(0, 0, 0, 0.2);
	}

	.cart-botton {
		position: relative;
	}

	.cart-botton .cart-number {
		width: 30rpx;
		height: 30rpx;
		background: rgba(255, 85, 0, 1);
		border-radius: 50%;
		position: absolute;
		right: 0;
		color: #ffffff;
		font-size: 20rpx;
		font-family: PingFang SC;
		font-weight: bold;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.copyspace {
		height: 80px;
		background-color: #fff;
	}

	.cart-content {
		padding-bottom: 158rpx;
	}

	.cart-banner {
		border-top-left-radius: 6rpx;
		border-top-right-radius: 6rpx;
		background-color: #f5f5f5;
		display: flex;
		align-items: center;
		padding: 15rpx 30rpx;
		justify-content: space-between;
	}

	.vertical-bar {
		display: block;
		width: 4rpx;
		height: 20rpx;
		background: #ffc324;
		border-radius: 2rpx;
	}

	.claer-icon {
		width: 30rpx;
		height: 30rpx;
		display: flex;
		align-items: center;
	}

	.legend {
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(153, 153, 153, 1);
		margin-left: 10rpx;
	}

	.cart-show {
		background-color: #fff;
		padding: 0 30rpx;
	}

	.cart-show .cart-list:not(:last-child) {
		border-bottom: 1rpx solid #e0e0e0;
	}

	.cart-list,
	.cart-count {
		display: flex;
		align-items: center;
	}

	.cart-reduce,
	.cart-add {
		width: 42rpx;
		height: 42rpx;
	}

	.cart-mess {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-right: ;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(51, 51, 51, 1);
		padding: 30rpx 20rpx 30rpx 0;
	}

	.number-input {
		text-align: center;
		width: 60rpx;
	}
</style>
